package com.qq.Servlet.bood;

import com.google.gson.Gson;
import com.qq.model.Book;
import com.qq.util.DBUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@WebServlet("/bood/update01")
public class BoodUpdateServlet extends HttpServlet {
    //获取某书的数据
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int id= Integer.parseInt(req.getParameter("id"));
        //查出这本书，填入到表单
        Connection con = DBUtil.getConnection();
        try{
            //问号代表给出的id
            PreparedStatement ps = con.prepareStatement("select *from book where id=?");
            ps.setInt(1,id);
            //ResultSet记录集，将数据存到里面去
            ResultSet rs = ps.executeQuery();
            if (rs.next()){
                //通过id进行查询获取下面的数据，这里是假设一定有书的
                //我们要使用就要将我们的数据给变量名
                int id1 = rs.getInt("id");
                String name = rs.getString("name");
                String author = rs.getString("author");
                double price = rs.getDouble("price");
                //这里是new一个Book对象
                Book book =new Book(id,name,author,price);
                //setAttribute在一个请求域里加一个请求参数
                //这里是将数据填写到修改的页面，因为el表达式可以直接用"book"双引号里的
                //req.setAttribute("book",book);

                //这里没有显示id但是在浏览器中又有id输出是因为我们在编辑按钮上带有id
                //resp.getWriter().print("{\"name\":" + "\""+ name + "\""+ ",\"author\":" + "\""+ author + "\""+ ",\"price\":" + price + "}");


                //这里使用Gson的jar包进行对编辑页面的数据输出，就避免了写那一长串字符
                Gson gson = new Gson();
                String json = gson.toJson(book);
                resp.getWriter().print(json);

                 resp.flushBuffer();
            }
        }catch (SQLException e){
            e.printStackTrace();
        }
    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //req.setCharacterEncoding("utf-8");
        //resp.setCharacterEncoding("text/html;charset=UTF-8");
//        resp.setCharacterEncoding("UTF-8");
//        resp.setHeader("content-type","text/html;charset=UTF-8");

        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        //将新数据进行提交
        String id  = req.getParameter("id");
        String name = req.getParameter("name");
        String author = req.getParameter("author");
        String price = req.getParameter("price");
        Connection con = DBUtil.getConnection();
        try{
            PreparedStatement ps = con.prepareStatement("update book set name =?,author = ?,price=? where  id=?");
            ps.setString(1,name);
            ps.setString(2,author);
            ps.setString(3,price);
            ps.setString(4,id);
            //数字num代表修改了多少行数据
            int num = ps.executeUpdate();
            System.out.println("===>"+num);
            if (num>0)
                resp.getWriter().write("ok");
            else
                resp.getWriter().write("fail");


            //resp.sendRedirect("index01");
        }catch (SQLException e){
            e.printStackTrace();
        }

    }
}
